package vip.zhy.faststart.framework.aspectj;


import jakarta.servlet.http.HttpServletRequest;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import vip.zhy.faststart.framework.json.JSON;
import vip.zhy.faststart.framework.spring.SpringUtil;

@Slf4j
@Aspect
@Component
public class WebLogAspect {

    @Pointcut("execution(* vip.zhy.faststart.*.controller..*.*(..))")
    private void webLog() {
    }

    @SneakyThrows
    @Around("webLog()")
    public Object around(ProceedingJoinPoint pjp) {

        long start = System.nanoTime();
        HttpServletRequest req = SpringUtil.getRequest();
        String requestedSessionId = req.getRequestedSessionId();

        log.info("ID : {}, URL : {}, HTTP_METHOD : {}, IP : {}, CLASS_METHOD : {}, ARGS : {}", requestedSessionId,
                req.getRequestURL(), req.getMethod(), req.getRemoteAddr(),
                pjp.getSignature().getDeclaringTypeName() + "." + pjp.getSignature().getName(), JSON.toJSON(req.getParameterMap()));

        Object r = pjp.proceed();

        long end = System.nanoTime();
        log.info("ID : {}, RESPONSE : {}, TIME(nano) : {}", requestedSessionId, r, end - start);

        return r;
    }

}
